#include <stdio.h>
#include <stdarg.h>

const int MAX_BUFF_SIZE = 1024;

/*
 * 求和
 * @param argc  参数个数
 * @param ...   可变参数列表
 * @return      所有参数的和
 */
int sum(int argc, ...) {
    int sum = 0, tmp;

    va_list argv;
    va_start(argv, argc); 

    for (int i = 0; i < argc; i++) {
        tmp = va_arg(argv, int);
        printf("%d\n", tmp);
        sum += tmp;
    }

    va_end(argv);
    return sum;
}

void myPrint(const char *errMsg, const char *fmt, ...) {
    va_list argv;
    char buff[MAX_BUFF_SIZE];

    va_start(argv, fmt);
    vsnprintf(buff, MAX_BUFF_SIZE - 1, fmt, argv);
    va_end(argv);

    printf("%s: %s\n", errMsg, buff);

}

int main() {
    printf("sum = %d\n", sum(3, 10, 20, 30));
    printf("sum = %d\n", sum(4, 40, 39, 38, 37));
    myPrint("Hello", "%s %d", "I am an error, my code is", 291);

    return 0;
}
